package com.hckjsec.law.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSON;
import com.hckjsec.common.core.domain.entity.SysDictData;
import com.hckjsec.common.utils.ComUtil;
import com.hckjsec.common.utils.SecurityUtils;
import com.hckjsec.system.service.ISysDictTypeService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.hckjsec.common.annotation.Log;
import com.hckjsec.common.core.controller.BaseController;
import com.hckjsec.common.core.domain.AjaxResult;
import com.hckjsec.common.enums.BusinessType;
import com.hckjsec.law.domain.WebLawCatalogue;
import com.hckjsec.law.service.IWebLawCatalogueService;
import com.hckjsec.common.utils.poi.ExcelUtil;
import com.hckjsec.common.core.page.TableDataInfo;

/**
 * 公共法律服务目录清单Controller
 * 
 * @author cyt
 * @date 2021-11-25
 */
@RestController
@RequestMapping("/system/lawcatalogue")
public class WebLawCatalogueController extends BaseController
{
    @Autowired
    private IWebLawCatalogueService webLawCatalogueService;

    @Autowired
    private ISysDictTypeService dictTypeService;

    /**
     * 查询公共法律服务目录清单列表
     */
    @GetMapping("/list")
    public TableDataInfo list(WebLawCatalogue webLawCatalogue)
    {
        startPage();
        Map<String,String> matterMap = new HashMap<String,String>();
        List<SysDictData> selectMatterByType=dictTypeService.selectDictDataByType(ComUtil.CONFIG_LAW_MATTER_TYPE);
        selectMatterByType.forEach(matterKv ->{
            matterMap.put(matterKv.getDictValue(),matterKv.getDictLabel());
        });
        Map<String,String> executeMap = new HashMap<String,String>();
        List<SysDictData> selectExecuteByType=dictTypeService.selectDictDataByType(ComUtil.CONFIG_LAW_EXECUTE_TYPE);
        selectExecuteByType.forEach(executeKv ->{
            executeMap.put(executeKv.getDictValue(),executeKv.getDictLabel());
        });
        List<WebLawCatalogue> list = webLawCatalogueService.selectWebLawCatalogueList(webLawCatalogue);
        list.forEach(kv->{
                    kv.setEventType(matterMap.get(kv.getEventType()));
                    kv.setHandlingType(executeMap.get(kv.getHandlingType()));
        });
        return getDataTable(list);
    }

    /**
     * 导出公共法律服务目录清单列表
     */
    @Log(title = "公共法律服务目录清单", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(WebLawCatalogue webLawCatalogue)
    {
        List<WebLawCatalogue> list = webLawCatalogueService.selectWebLawCatalogueList(webLawCatalogue);
        ExcelUtil<WebLawCatalogue> util = new ExcelUtil<WebLawCatalogue>(WebLawCatalogue.class);
        return util.exportExcel(list, "catalogue");
    }

    /**
     * 获取公共法律服务目录清单详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(webLawCatalogueService.selectWebLawCatalogueById(id));
    }

    /**
     * 新增公共法律服务目录清单
     */
    @Log(title = "公共法律服务目录清单", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody WebLawCatalogue webLawCatalogue)
    {
        webLawCatalogue.setDeptId(SecurityUtils.getDeptId());
        webLawCatalogue.setCreateBy(SecurityUtils.getUsername());
        return toAjax(webLawCatalogueService.insertWebLawCatalogue(webLawCatalogue));
    }

    /**
     * 修改公共法律服务目录清单
     */
    @Log(title = "公共法律服务目录清单", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody WebLawCatalogue webLawCatalogue)
    {
        webLawCatalogue.setUpdateBy(SecurityUtils.getUsername());
        return toAjax(webLawCatalogueService.updateWebLawCatalogue(webLawCatalogue));
    }

    /**
     * 删除公共法律服务目录清单
     */
    @Log(title = "公共法律服务目录清单", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(webLawCatalogueService.deleteWebLawCatalogueByIds(ids));
    }
}
